﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace YiSha.SqlSugars
{
    public class FileHelper
    {
		public static void CreateFile(string filePath, string text, Encoding encoding)
		{
			try
			{
				bool flag = FileHelper.IsExistFile(filePath);
				if (flag)
				{
					FileHelper.DeleteFile(filePath);
				}
				bool flag2 = !FileHelper.IsExistFile(filePath);
				if (flag2)
				{
					string directoryFromFilePath = FileHelper.GetDirectoryFromFilePath(filePath);
					FileHelper.CreateDirectory(directoryFromFilePath);
					FileInfo fileInfo = new FileInfo(filePath);
					using (FileStream fileStream = fileInfo.Create())
					{
						using (StreamWriter streamWriter = new StreamWriter(fileStream, encoding))
						{
							streamWriter.Write(text);
							streamWriter.Flush();
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static bool IsExistDirectory(string directoryPath)
		{
			return Directory.Exists(directoryPath);
		}

		public static void CreateDirectory(string directoryPath)
		{
			bool flag = !FileHelper.IsExistDirectory(directoryPath);
			if (flag)
			{
				Directory.CreateDirectory(directoryPath);
			}
		}

		public static void DeleteFile(string filePath)
		{
			bool flag = FileHelper.IsExistFile(filePath);
			if (flag)
			{
				File.Delete(filePath);
			}
		}

		public static string GetDirectoryFromFilePath(string filePath)
		{
			FileInfo fileInfo = new FileInfo(filePath);
			DirectoryInfo directory = fileInfo.Directory;
			return directory.FullName;
		}

		public static bool IsExistFile(string filePath)
		{
			return File.Exists(filePath);
		}
	}
}
